package com.test.flink_transforms;

import com.test.beans.UserBean;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class F01Map {

    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port", 8888);
        StreamExecutionEnvironment webUI = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);

        DataStreamSource<String> data = webUI.socketTextStream("127.0.0.1", 8899);

        DataStream<UserBean> ds = data.map(new MapFunction<String, UserBean>() {

            @Override
            public UserBean map(String line) throws Exception {
                // 数据在解析的时候可能会抛出异常 需要捕获异常防止程序停止  trycatch 快捷键 ctr+alt+t
                UserBean userBean = new UserBean();
                try {
                    String[] arr = line.split(",");
                    userBean.setUid(Integer.parseInt(arr[0]));
                    userBean.setName(String.valueOf(arr[1]));
                    userBean.setAge(Integer.parseInt(arr[2]));
                    userBean.setAddress(String.valueOf(arr[3]));
                    userBean.setBirthday(Long.parseLong(arr[4]));
                } catch (Exception e) {

                }
                return userBean;
            }
        });

        ds.print();
        webUI.execute();
    }
}
